package com.hncu.edu.info.manager.controller;

import com.hncu.edu.info.manager.domain.Teacher;
import com.hncu.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public abstract class BaseTeacherController {
    Scanner in = new Scanner(System.in);
    TeacherService teacherService = new TeacherService();

    //开启老师管理系统
    public final void start() {
        lo:
        while (true) {
            System.out.println("---------欢迎来到湖南城市学院老师管理系统---------");
            System.out.println("请输入您的选择：1、添加老师 2、删除老师 3、修改老师 4、查看老师 5、退出");
            //接收选择
            String choice = in.next();
            //选择路线
            switch (choice) {
                case "1":
//                    System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
//                    System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
//                    System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
//                    System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用湖南城市学院老师管理系统，再见！");
                    break lo;
                default:
                    System.out.println("您输入有误，请重新输入！");
                    break;
            }
        }
    }

    //修改老师
    public final void updateTeacher() {
        //获得Id
        String updateId = CheckId();
        //获得对象
        Teacher teacher = getTeacher(updateId);
        teacherService.upadateTeacher(teacher);
        System.out.println("修改成功");
    }

    //删除老师
    public final void deleteTeacher() {
        String deleteId;
        while (true) {
            System.out.println("请输入工号：");
            deleteId = in.next();
            //接收布尔值，判断工号是否存在数组中
            boolean flag = teacherService.isExist(deleteId);
            //如果存在，跳出删除
            if (flag) {
                break;
            } else {
                System.out.println("查无此人，请重新输入！");
            }
        }
        teacherService.deleteTeacher(deleteId);
        System.out.println("删除成功");
    }

    //查找老师
    public final void findAllTeacher() {
        //获取数组地址
        Teacher[] arr = teacherService.findAllTeacher();
        //如果没有做添加老师的步骤，那么数组的地址是不存在的
        if (arr != null) {
            System.out.println("工号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < arr.length; i++) {
                Teacher teacher = arr[i];
                //要先判断是否为空，不然会报错
                if (teacher != null) {
                    System.out.println(teacher.getid() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
                }
            }
        } else {
            System.out.println("查无信息，请添加后重试！");
        }
    }

    // 添加老师
    public final void addTeacher() {
        String addId;
        while (true) {
            System.out.println("请输入工号：");
            //接收Id,判断Id是否存在
            addId = in.next();
            //接收返回值
            boolean flag = teacherService.isExist(addId);
            //返回值为true，工号不能两个老师用
            if (flag) {
                System.out.println("工号被占用，请重新输入");
            }
            //返回值为false则跳出循环，继续完善其他信息
            else {
                break;
            }
        }
        //获得对象
        Teacher teacher = getTeacher(addId);
        //将对象传出
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //查询，判断工号是否在数组内
    public final String CheckId() {
        String Id;
        while (true) {
            System.out.println("请输入工号：");
            Id = in.next();
            //接受布尔值,判断工号是否在数组内
            boolean flag = teacherService.isExist(Id);
            //如果工号在数组内，跳出循环，修改其他信息
            if (flag) {
                break;
            } else {
                System.out.println("查无此人，请重新输入!");
            }
        }
        return Id;
    }

    //信息录入与封装
    public abstract Teacher getTeacher(String Id);
}
